package com.cda.controller;

import com.cda.common.BaseLogger;
import com.cda.dao.MonneyInfoLogDao;
import com.cda.entity.MonneyInfoLog;
import com.cda.entity.Person;
import com.cda.entity.SfdevicebindEntity;
import com.cda.service.FaceService;
import com.cda.service.PosInputInfoSevice;
import com.cda.service.SfdevicebindService;
import com.cda.utils.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;


@Controller
@RequestMapping("/pos")
public class PosController extends BaseLogger {
	@Value("${pos.log.url}")
	private String dataFileUrl;
	static List<MonneyInfoLog> monneyInfoLogList=new ArrayList<MonneyInfoLog>();
	@Autowired
	private SfdevicebindService sfdevicebindService;
	@Autowired
	FaceService faceService;
	@Autowired
	PosInputInfoSevice posInputInfoSevice;
	@Autowired
	private MonneyInfoLogDao monneyInfoLogDao;
	@RequestMapping(value="/photo",method = RequestMethod.GET)
	@ResponseBody
	public String photo(){

		Person p = new Person();
		p.setId("001");//ID,验证人员唯一性的字段作
		p.setName("人脸识别成功");//人员的名字
		p.setIC_NO("1001");//为维根号发送到闸机
		p.setID_NO("4409231991064444111");//	员工身份证号
		faceService.photograph(p);

		return null;
	}


	@RequestMapping(value="/getMoney",produces="application/json;charset=utf-8",method = RequestMethod.POST)
	@ResponseBody
	public String listRecord(String localIp){
	System.out.println(localIp);
		logger.info("执行解析人脸识别回调函数");
		SfdevicebindEntity sfdevicebindEntity =sfdevicebindService.findByClientIp(getHostIp());
		if( sfdevicebindEntity == null ){
			logger.info("设备绑定表数据为null，请配置");
		}
		String aStri="";
		for(int i=0;i<(12-sfdevicebindEntity.getPosSum().length());i++){
			aStri+="0";
		}
		return aStri+sfdevicebindEntity.getPosSum();
	}

	@RequestMapping(value="/saveLog",produces="application/json;charset=utf-8",method = RequestMethod.POST)
	@ResponseBody
	public String saveLog(String outStr) throws Exception{
		MonneyInfoLog monneyInfoLog=new MonneyInfoLog();
		if(StringUtils.isNotEmpty(outStr)){
			//获取返回码
			String respCode=outStr.substring(0,2).trim();
			String resp_chin=outStr.substring(127,173);
			monneyInfoLog.setRespCode(respCode);
			monneyInfoLog.setRespChin(resp_chin.trim());
			//交易成功爆出数据开闸门
			if("00".equals(respCode)){
				String bank_code=outStr.substring(2,6);
				String card_no=outStr.substring(6,36);
				String expr=outStr.substring(36,40);
				String  amount=outStr.substring(40,52);
				String  trace=outStr.substring(52,58);
				String  refer=outStr.substring(58,70);
				String  auth=outStr.substring(70,76);
				String  batch=outStr.substring(76,82);
				String  date=outStr.substring(82,96);
				String  userno=outStr.substring(96,111);
				String  terno=outStr.substring(111,119);
				String old_terno=outStr.substring(119,127);
				String  OrderNo=outStr.substring(173,222);
				String  lrc=outStr.substring(222,225);
				monneyInfoLog.setBankCode(bank_code);
				monneyInfoLog.setCardNo(card_no);
				monneyInfoLog.setExprDate(expr);
				monneyInfoLog.setAmount(Integer.parseInt(amount) + "");
				monneyInfoLog.setTraceNo(trace);
				monneyInfoLog.setRefer(refer);
				monneyInfoLog.setAuthNo(auth);
				monneyInfoLog.setBatchNo(batch);
				monneyInfoLog.setPayDate(org.apache.commons.lang3.time.DateUtils.parseDate(DateFormatUtils.format(new Date(), "yyyymmdHHhmmss"), "yyyymmdHHhmmss"));
				monneyInfoLog.setUserno(userno);
				monneyInfoLog.setTerno(terno);
				monneyInfoLog.setOldTerno(old_terno);
				monneyInfoLog.setOrderno(OrderNo);
				monneyInfoLog.setLrc(lrc);
				monneyInfoLog.setCreateDate(new Date());
				SfdevicebindEntity sfdevicebindEntity1 =sfdevicebindService.findByClientIp(getHostIp());
				monneyInfoLog.setDeviceNo(sfdevicebindEntity1.getName());
				//保持交易日志
				try {
					monneyInfoLog.setId(UUID.randomUUID().toString());
					monneyInfoLogDao.save(monneyInfoLog);
				}catch (Exception e){
					try {
						//保存数据失败将数据放到本地
						//每一次都会清空原来的数据
						monneyInfoLogList.add(monneyInfoLog);
						FileOutputStream fos=new FileOutputStream(dataFileUrl);
						ObjectOutputStream oos = new ObjectOutputStream(fos);
						oos.writeObject(monneyInfoLogList);
						System.out.println(monneyInfoLogList);
						oos.close();
					}catch (Exception xe){
						logger.error("保存数据到本地异常");
						xe.printStackTrace();
					}
				}
				boolean flag2  = posInputInfoSevice.control(monneyInfoLog);//开闸

			}


		}
		return "成功了";
	}

	public String getHostIp() {
		InetAddress addr = null;
		String clientIp = "";
		try {
			addr = InetAddress.getLocalHost();
			clientIp = addr.getHostAddress().toString(); //获取本机ip
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		return clientIp;
	}

}